/**
 * @Desciption 校验规则维护JS-systemValidation.jsp
 * @Author guoyanfei
 * @date 2018/01/05
 */
var validation = function(){
	var strPath = window.document.location.pathname;
    //var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    var postPath = "/gdp";
	
	//查询所有校验规则
	var getAllvalidation = function(seachTxt){
		//加载数据
		$("#tb_dataList").bootstrapTable('destroy').bootstrapTable({
			url : postPath + '/system/sysvalidation/getAllValidation',
		    undefinedText : '-',
		    striped : true, // 是否显示行间隔色  
		    queryParams : {seachTxt:seachTxt}, 
		    uniqueId : "guid", // 每一行的唯一标识 
		    columns:[{
		    	field : 'state',  
		        checkbox : true,  
		        align : 'center',  
		        valign : 'middle',
		        width : '40px'
		    },{
		    	title : '校验规则Code',  
		        field : 'validatecode',  
		        align : 'center', 
		        valign : 'middle',
		        width : '120px'
		    },{
		    	title : '校验规则名称',  
		        field : 'validatename',  
		        align : 'center', 
		        valign : 'middle',
		        width : '180px'
		    },{
		    	title : '校验值',  
		        field : 'validatevalue',  
		        align : 'center', 
		        valign : 'middle',
		        width : '400px'
		    },{
		    	title : '描述信息',  
		        field : 'description',  
		        align : 'center', 
		        valign : 'middle',
		        width : '450px'
		    }]
		});
		
		//查询
		$("#btn_Search").click(function(){
			getAllvalidation($("#seachTxt").val());
		});
	};
	
	//维护信息
	var editInfo={
			eventAdd:function(){//新增信息
				$("#btn_add").click(function(){
					editInfo.cleanData();
					$("#EditContent").modal("show");
				});
			},
			eventUpdate:function(){//修改信息
				$("#btn_edit").click(function(){
					editInfo.cleanData();
					var row = $('#tb_dataList').bootstrapTable('getSelections');
			        if (row == null || row == "") {
			            bootbox.alert("请选择一行!");
			            return;
			        }
			        var validatecode = row[0].validatecode;
					$.ajax({
						url:postPath+"/system/sysvalidation/getValidationByCode",
						type:"post",
						data:{validatecode:validatecode},
						success:function(data){
							if(data !=null){
								$("#validatecode").attr("readonly","true");
								$("#validatecode").val(data.validatecode);
								$("#hid_validatecode").val(data.validatecode);
								$("#validatename").val(data.validatename);
								$("#validatevalue").val(data.validatevalue);
								$("#description").val(data.description);
								$("#EditContent").modal("show");
							}
						}
					});
					
				});
			},
			eventDel:function(){//删除信息
				$("#btn_del").click(function(){
					var row = $('#tb_dataList').bootstrapTable('getSelections');
			        if (row == null || row == "") {
			            bootbox.alert("请最少选择一行!");
			            return;
			        }
			        var guids="";
					var message = "";
			        $.each(row,function(i,e){
			        	if(guids == ""){
			        		guids+=e.validatecode;
			        	}else{
			        		guids += ","+e.validatecode;
			        	}

						message += e.validatecode + "," +e.validatename + "<br/>"
			        });

					var deleteFunction = function(result){
			        	if(result== false){
			        		return;
			        	}

			        	$.ajax({
			        		url:postPath+"/system/sysvalidation/delValidation",
			        		type:"post",
			        		data:{guids:guids},
			        		success:function(data){
			        			if(data > 0){
									showMsg("删除成功!","success");
			        				getAllvalidation($("#seachTxt").val());
			        			}else{
									showMsg("删除失败,请刷新后重试!","error");
			        			}
			        		}
			        	});
			        };
			        
			        confirmDelete(message, deleteFunction);
			        
			        
				});
			},
			eventSave:function(){//保存信息
				$("#btn_save").click(function(){
					var validateResult = editInfo.validateInfo();
					if(validateResult){
						var editurl="";
						if($("#hid_validatecode").val() ==""){
							editurl = postPath+"/system/sysvalidation/addValidation";
						}else{
							editurl = postPath+"/system/sysvalidation/updateValidation";
						}
						var temp={
								validatecode:$("#validatecode").val(),
								validatename:$("#validatename").val(),
								validatevalue:$("#validatevalue").val(),
								description:$("#description").val()
						};
						$.ajax({
							url:editurl,
							type:"post",
							data:JSON.stringify(temp),
							dataType : 'json',
			                contentType:"application/json",
							success:function(res){
								if(res > 0){
									showMsg("保存成功!","success");
									getAllvalidation($("#seachTxt").val());
									$("#EditContent").modal("hide");
								}else{
									showMsg("保存失败,请刷新后重试!","error");
								}
							}
						});
					}
				});
			},
			eventRefresh:function(){
				$("#btn_ref").click(function(){
					getAllvalidation($("#seachTxt").val());
				});
			},
			validateInfo:function(){//校验信息
				if($("#validatecode").val() == ""){
					$("#errorMessage").html("校验规则code不能为空,且唯一!");
					return false;
				}else{
					if($("#hid_validatecode").val() ==""){
						//新增，验证是否唯一
						$.ajax({
							url:postPath+"/system/sysvalidation/isExitValidateCode",
							type:"post",
							data:{validateCode:$("#validatecode").val()},
							async:false,
							success:function(res){
								if(res){
									$("#errorMessage").html("校验规则code已存在,请重新输入!");
									return false;
								}else{
									$("#errorMessage").html("");
								}
							}
						});
					}else{
						$("#errorMessage").html("");
					}
				}
				
				if($("#validatename").val() == ""){
					$("#errorMessage").html("请输入校验规则名称!");
					return false;
				}else{
					$("#errorMessage").html("");
				}
				
				if($("#validatevalue").val() == ""){
					$("#errorMessage").html("请输入校验规值!");
					return false;
				}else{
					$("#errorMessage").html("");
				}
				
				return true;
			},
			cleanData:function(){//清空信息
				$("#validatecode").val("");
				$("#validatecode").removeAttr("readonly");
				$("#hid_validatecode").val("");
				$("#validatename").val("");
				$("#validatevalue").val("");
				$("#description").val("");
			},
			init:function(){//初始化
				editInfo.eventAdd();
				editInfo.eventSave();
				editInfo.eventDel();
				editInfo.eventUpdate();
				editInfo.eventRefresh();
			}
	};
	
	return{
		init:function(){
			getAllvalidation();
			editInfo.init();
		}
	};
	
}();

$(function(){
	validation.init();
})

function showMsg(text, icon) {
	$.toast().reset('all');
	$.toast({
		text: text,
		icon: icon,
		showHideTransition: 'fade',
		allowToastClose: true,
		hideAfter: 1500,
		stack: false,
		position: 'top-right',
		textAlign: 'left',
		loader: true,
		loaderBg: '#9ec600',
	});
}

function confirmDelete(message, confirmFunction){
    var messageObj = {
        title: '确认删除？',
        message: message ,
        callback: confirmFunction
    }
    bootbox.confirm(messageObj);
}
